package com.rc.saas.tenant.service.hlht;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rc.saas.tenant.base.BaseSearch;
import com.rc.saas.tenant.mapper.hlht.HlhtTenantOpenApplyMapper;
import com.rc.saas.tenant.model.hlht.*;
import com.rc.saas.tenant.model.hlhtsk.*;
import com.rc.saas.tenant.model.operator.TenantOperator;
import com.rc.saas.tenant.model.operator.TenantOperatorExample;
import com.rc.saas.tenant.search.hlht.HlhtTenantOpenApplySearch;
import com.rc.saas.tenant.service.hlhtsk.*;
import com.rc.saas.tenant.service.operator.TenantOperatorService;
import com.rc.saas.tenant.vo.hlht.HlhtTenantOpenApplyVo;
import com.rc.saas.tenant.vo.hlht.HlhtTenantVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author frog
 * @date 2021/4/23
 */
@Service
public class HlhtTenantOpenApplyServiceImpl implements HlhtTenantOpenApplyService {
    @Autowired
    private HlhtTenantOpenApplyMapper hlhtTenantOpenApplyMapper;

    @Autowired
    private HlhtOperatorSettingService hlhtOperatorSettingService;

    @Autowired
    private HlhtTenantSettingService hlhtTenantSettingService;

    @Autowired
    private HlhtTenantOpenService hlhtTenantOpenService;

    @Autowired
    HlhtOperatorService hlhtOperatorService;

    @Autowired
    HlhtskBizTenantService hlhtskBizTenantService;

    @Autowired
    HlhtskBizOperatorService hlhtskBizOperatorService;


    @Autowired
    HlhtskGovtTenantService hlhtskGovtTenantService;

    @Autowired
    TenantOperatorService tenantOperatorService;

    @Autowired
    HlhtskGovtTenantOperatorService hlhtskGovtTenantOperatorService;

    @Autowired
    HlhtskGovtOperatorService hlhtskGovtOperatorService;

    @Override
    public HlhtTenantOpenApplyMapper getMapper() {
        return hlhtTenantOpenApplyMapper;
    }

    @Override
    public PageInfo<HlhtTenantOpenApplyVo> listPageHlhtTenantOpenApply(HlhtTenantOpenApplySearch hlhtTenantOpenApplySearch) {
        PageHelper.startPage(hlhtTenantOpenApplySearch.getOffset(), hlhtTenantOpenApplySearch.getLimit());
        List<HlhtTenantOpenApplyVo> hlhtTenantOpenApplies = hlhtTenantOpenApplyMapper.listPageHlhtTenantOpenApply(hlhtTenantOpenApplySearch);
        return new PageInfo<HlhtTenantOpenApplyVo>(hlhtTenantOpenApplies);
    }

    @Override
    public void addTenantAndOperatorAndOpen(HlhtTenantOpenApply hlhtTenantOpenApply) {
        String applyTenantCode = hlhtTenantOpenApply.getTenantCode();//运营商
        String applyHlhtTenanCode = hlhtTenantOpenApply.getHlhtTenantCode();//互联互通内部运营商
        String applyOperatorId = hlhtTenantOpenApply.getHlhtOperatorId();//互联互通外部运营商
        String applyTenantOPeratorCode = hlhtTenantOpenApply.getTenantOperatorCode();//分销商
        String hlhtOpenType = hlhtTenantOpenApply.getHlhtOpenType();//互联类型
        //hlht_type 申请的互联互通类型,以tenant_code为基准，0反向对接 1正向对接  默认0
        String hlhtType = hlhtTenantOpenApply.getHlhtType();//互联方向

        if(hlhtType.equals("0")){
            //填充HlhtOperatorSetting(反向)
            HlhtOperatorSetting hlhtOperatorSetting = new HlhtOperatorSetting();
            hlhtOperatorSetting.setTenantCode(applyTenantCode);
            hlhtOperatorSetting.setHlhtOperatorId(applyOperatorId);
            hlhtOperatorSettingService.insertSelective(hlhtOperatorSetting);

        }else if (hlhtType.equals("1")){
            //填充HlhtTenantSetting(正向)
            HlhtTenantSetting hlhtTenantSetting = new HlhtTenantSetting();
            hlhtTenantSetting.setTenantCode(applyTenantCode);
            hlhtTenantSetting.setHlhtTenantCode(applyHlhtTenanCode);
            hlhtTenantSetting.setHlhtOperatorId(applyOperatorId);
            hlhtTenantSettingService.insertSelective(hlhtTenantSetting);//新增互通的api信息
        }

        //填充HlhtTenantOpen
        HlhtTenantOpen hlhtTenantOpen = new HlhtTenantOpen();
        hlhtTenantOpen.setTenantCode(applyTenantCode);
        hlhtTenantOpen.setTenantOperatorCode(applyTenantOPeratorCode);
        hlhtTenantOpen.setHlhtOperatorId(applyOperatorId);
        hlhtTenantOpen.setHlhtTenantCode(applyHlhtTenanCode);
        hlhtTenantOpen.setHlhtOpenType(hlhtOpenType);
        hlhtTenantOpen.setHlhtType(hlhtType);
        hlhtTenantOpen.setJoinType(hlhtTenantOpenApply.getJoinType());
        hlhtTenantOpen.setJoinBrandType(hlhtTenantOpenApply.getJoinBrandType());
        hlhtTenantOpen.setSettleType(hlhtTenantOpenApply.getSettleType());
        hlhtTenantOpen.setPayTenantCode(hlhtTenantOpenApply.getPayTenantCode());
        hlhtTenantOpen.setBeginDate(hlhtTenantOpenApply.getBeginDate());
        hlhtTenantOpen.setEndDate(hlhtTenantOpenApply.getEndDate());
        hlhtTenantOpenService.insertSelective(hlhtTenantOpen);//新增互联互通信息

        //0 SAAS运营商 1商业运营平台 2政府平台
        if (hlhtOpenType.equals("2")){
            //填充HlhtskGovtTenant，政务互联时运营商的密钥信息
            HlhtskGovtTenant skGovtTenant = new HlhtskGovtTenant();
            skGovtTenant.setTenantCode(applyTenantCode);
            skGovtTenant.setHlhtOperatorId(applyOperatorId);
            hlhtskGovtTenantService.insertSelective(skGovtTenant);//新增对接政府时的运营商密钥数据
            //根据tenantcode，来获取该运营商的分销商list，政务互联时分销商的密钥
            TenantOperatorExample tenantOperatorExample = new TenantOperatorExample();
            tenantOperatorExample.createCriteria().andTenantCodeEqualTo(applyTenantCode);
            List<TenantOperator> tenantOperatorList = tenantOperatorService.selectByExample(tenantOperatorExample);
            //有多少个分销商，就新增多少个分销商互联政府的分销商密钥信息
            if (tenantOperatorList.size() > 0){
                for (TenantOperator tenantOperator:tenantOperatorList) {
                    HlhtskGovtTenantOperator skGovtTenOper = new HlhtskGovtTenantOperator();
                    skGovtTenOper.setTenantCode(tenantOperator.getTenantCode());
                    skGovtTenOper.setTenantOperatorCode(tenantOperator.getTenantOperatorCode());
                    skGovtTenOper.setHlhtOperatorId(applyOperatorId);
                    skGovtTenant.setExpiresTime(Long.valueOf(0));
                    skGovtTenant.setOperatorSecret("");
                    skGovtTenant.setSigSecret("");
                    skGovtTenant.setDataSecret("");
                    skGovtTenant.setDataSecretiv("");
                    hlhtskGovtTenantOperatorService.insertSelective(skGovtTenOper);
                }
            }
            //当审核通过后，增加政府类密钥数量为n+1  (n：分销商的数量； 1：运营商)
            //增加政府类的密钥（互联运营商时）
            HlhtskGovtOperator hlhtskGovtOperator = new HlhtskGovtOperator();
            hlhtskGovtOperator.setTenantCode(applyTenantCode);
            hlhtskGovtOperator.setHlhtOperatorId(applyOperatorId);
            hlhtskGovtOperator.setHlhtType(hlhtType);
            hlhtskGovtOperatorService.insertSelective(hlhtskGovtOperator);
            //增加政府类的密钥(互联分销商时)
            if (tenantOperatorList.size() > 0){
                for (int i = 0; i < tenantOperatorList.size(); i++){
                    HlhtskGovtOperator hlhtskGovtOperatorTen = new HlhtskGovtOperator();
                    hlhtskGovtOperatorTen.setTenantCode(applyTenantCode);
                    hlhtskGovtOperatorTen.setHlhtOperatorId(applyOperatorId);
                    hlhtskGovtOperatorTen.setTenantOperatorCode(tenantOperatorList.get(i).getTenantOperatorCode());
                    hlhtskGovtOperatorTen.setHlhtType(hlhtType);
                    hlhtskGovtOperatorService.insertSelective(hlhtskGovtOperatorTen);
                }
            }
        }else if (hlhtOpenType.equals("1")){
            //填充HlhtskBizTenant,商业互联时设备商的密钥
            HlhtskBizTenant skTenant = new HlhtskBizTenant();
            skTenant.setTenantCode(applyTenantCode);
            skTenant.setHlhtOperatorId(applyOperatorId);
            skTenant.setOperatorSecret("");
            skTenant.setSigSecret("");
            skTenant.setDataSecret("");
            skTenant.setDataSecretiv("");
            hlhtskBizTenantService.insertSelective(skTenant);//新增设备商的密钥数据

            //填充HlhtskBizOperator, 商业互联时运营商的密钥
            HlhtskBizOperator skOperator = new HlhtskBizOperator();
            skOperator.setTenantCode(applyTenantCode);
            skOperator.setHlhtOperatorId(applyOperatorId);
            skOperator.setHlhtType(hlhtType);
            if (hlhtType.equals("0")){
                skOperator.setMemberId(Long.valueOf(0));//会员id，反向时才需要
            }
            hlhtskBizOperatorService.insertSelective(skOperator);//新增对接商业运营商的的运营商密钥数据
        }
    }

    @Override
    public List<HlhtTenantVo> listHlhtTenant(BaseSearch baseSearch) {
        return hlhtTenantOpenApplyMapper.listHlhtTenant(baseSearch);
    }
}
